//
//  HtmlCell.m
//  MAdaptorV2
//
//  Created by cuiyuguo on 11-7-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HtmlCell.h"
#import "MeetingDetailViewController.h"

@implementation HtmlCell
@synthesize webView = _webView,tableviewHeights = _tableviewHeights,row = _row,section = _section;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		_webView = [[UIWebView alloc] initWithFrame:self.bounds];
		_webView.autoresizingMask = AutoresizingFull;
        _webView.scalesPageToFit = YES;
		
		_webView.userInteractionEnabled = NO;
		_webView.backgroundColor = [UIColor clearColor];
		_webView.opaque = NO;
		_webView.delegate = self;
		[self addSubview:_webView];
		
		//添加观察者，当有html数据被载入过程中，contentSize属性值会发生变化
		for (UIView* subView in _webView.subviews) {
			if ([subView isKindOfClass:[UIScrollView class]]) {
				_scrollView = (UIScrollView*)subView;
				break;
			}
		}
    }
    return self;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {

        _webView.userInteractionEnabled = YES;
        CGFloat high=0.0;
        NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",200];
        [webView stringByEvaluatingJavaScriptFromString:jsString];
        high = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"].floatValue/2.3f;
        CGRect frame = [webView frame];
        frame.size.height = high;
        [webView setFrame:CGRectMake(5, 10, 310, high)];
        
        
        CGRect cellFrame = [self frame];
        cellFrame.size.height =  high;
        [self setFrame:cellFrame];
        self.cellHeight =  high;
        
        
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:self.row inSection:self.section];
        
        if (!((MeetingDetailViewController*)self.superview.viewController).cellHeight || !((MeetingDetailViewController*)self.superview.viewController).cellHeight == high) {
            ((MeetingDetailViewController*)self.superview.viewController).cellHeight =high;
            [(UITableView*)self.superview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                                withRowAnimation:UITableViewRowAnimationNone];
        
    }
}



- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"html加载错误");
}


@end
